#include <tchar.h>
#include <Windows.h>

void ReadValue(DWORD dwIndex)
{
	TCHAR* szContent = (TCHAR*)TlsGetValue(dwIndex);
	if (NULL != szContent)
	{
		DWORD dwWrote = 0;
		OutputDebugString(szContent);
	}

	return;
}

DWORD CALLBACK ThreadProc(LPVOID lpData)
{
	DWORD dwIndex = *(DWORD*)lpData;
	TCHAR* szContent = new TCHAR[256];
	_stprintf(szContent, _T("I Come from 0x%04X\n"), GetCurrentThreadId());
	TlsSetValue(dwIndex, szContent);

	ReadValue(dwIndex);

	delete[] szContent;

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hPreInstance, HINSTANCE hInstance, LPTSTR szCmdLine, INT nShowCmd)
{		
	const UINT dwCount = 4;
	HANDLE hThreads[dwCount];
	DWORD dwIndex = TlsAlloc();
	if (dwIndex != TLS_OUT_OF_INDEXES)
	{	
		for (int ii = 0; ii < dwCount; ii++)
		{
			hThreads[ii] = CreateThread(NULL, 0, ThreadProc, (LPVOID)&dwIndex, 0, 0);
		}
	}
	
	WaitForMultipleObjects(dwCount, hThreads, TRUE, INFINITE);
	TlsFree(dwIndex);
	system("pause");
	return FALSE;
}